#pragma once
#include <QImage>
#include <windows.h>
#include "RayCiPro_C.h"
namespace RayCiLut {

   inline BYTE LUT[256][4] = {
        0,0,131,255,
        0,0,135,255,
        0,0,139,255,
        0,0,143,255,
        0,0,147,255,
        0,0,151,255,
        0,0,155,255,
        0,0,159,255,
        0,0,163,255,
        0,0,167,255,
        0,0,171,255,
        0,0,175,255,
        0,0,179,255,
        0,0,183,255,
        0,0,187,255,
        0,0,191,255,
        0,0,195,255,
        0,0,199,255,
        0,0,203,255,
        0,0,207,255,
        0,0,211,255,
        0,0,215,255,
        0,0,219,255,
        0,0,223,255,
        0,0,227,255,
        0,0,231,255,
        0,0,235,255,
        0,0,239,255,
        0,0,243,255,
        0,0,247,255,
        0,0,251,255,
        0,0,255,255,
        0,4,255,255,
        0,8,255,255,
        0,12,255,255,
        0,16,255,255,
        0,20,255,255,
        0,24,255,255,
        0,28,255,255,
        0,32,255,255,
        0,36,255,255,
        0,40,255,255,
        0,44,255,255,
        0,48,255,255,
        0,52,255,255,
        0,56,255,255,
        0,60,255,255,
        0,64,255,255,
        0,68,255,255,
        0,72,255,255,
        0,76,255,255,
        0,80,255,255,
        0,84,255,255,
        0,88,255,255,
        0,92,255,255,
        0,96,255,255,
        0,100,255,255,
        0,104,255,255,
        0,108,255,255,
        0,112,255,255,
        0,116,255,255,
        0,120,255,255,
        0,124,255,255,
        0,128,255,255,
        0,131,255,255,
        0,135,255,255,
        0,139,255,255,
        0,143,255,255,
        0,147,255,255,
        0,151,255,255,
        0,155,255,255,
        0,159,255,255,
        0,163,255,255,
        0,167,255,255,
        0,171,255,255,
        0,175,255,255,
        0,179,255,255,
        0,183,255,255,
        0,187,255,255,
        0,191,255,255,
        0,195,255,255,
        0,199,255,255,
        0,203,255,255,
        0,207,255,255,
        0,211,255,255,
        0,215,255,255,
        0,219,255,255,
        0,223,255,255,
        0,227,255,255,
        0,231,255,255,
        0,235,255,255,
        0,239,255,255,
        0,243,255,255,
        0,247,255,255,
        0,251,255,255,
        0,255,255,255,
        4,255,255,255,
        8,255,251,255,
        12,255,247,255,
        16,255,243,255,
        20,255,239,255,
        24,255,235,255,
        28,255,231,255,
        32,255,227,255,
        36,255,223,255,
        40,255,219,255,
        44,255,215,255,
        48,255,211,255,
        52,255,207,255,
        56,255,203,255,
        60,255,199,255,
        64,255,195,255,
        68,255,191,255,
        72,255,187,255,
        76,255,183,255,
        80,255,179,255,
        84,255,175,255,
        88,255,171,255,
        92,255,167,255,
        96,255,163,255,
        100,255,159,255,
        104,255,155,255,
        108,255,151,255,
        112,255,147,255,
        116,255,143,255,
        120,255,139,255,
        124,255,135,255,
        128,255,131,255,
        131,255,128,255,
        135,255,124,255,
        139,255,120,255,
        143,255,116,255,
        147,255,112,255,
        151,255,108,255,
        155,255,104,255,
        159,255,100,255,
        163,255,96,255,
        167,255,92,255,
        171,255,88,255,
        175,255,84,255,
        179,255,80,255,
        183,255,76,255,
        187,255,72,255,
        191,255,68,255,
        195,255,64,255,
        199,255,60,255,
        203,255,56,255,
        207,255,52,255,
        211,255,48,255,
        215,255,44,255,
        219,255,40,255,
        223,255,36,255,
        227,255,32,255,
        231,255,28,255,
        235,255,24,255,
        239,255,20,255,
        243,255,16,255,
        247,255,12,255,
        251,255,8,255,
        255,255,4,255,
        255,255,0,255,
        255,251,0,255,
        255,247,0,255,
        255,243,0,255,
        255,239,0,255,
        255,235,0,255,
        255,231,0,255,
        255,227,0,255,
        255,223,0,255,
        255,219,0,255,
        255,215,0,255,
        255,211,0,255,
        255,207,0,255,
        255,203,0,255,
        255,199,0,255,
        255,195,0,255,
        255,191,0,255,
        255,187,0,255,
        255,183,0,255,
        255,179,0,255,
        255,175,0,255,
        255,171,0,255,
        255,167,0,255,
        255,163,0,255,
        255,159,0,255,
        255,155,0,255,
        255,151,0,255,
        255,147,0,255,
        255,143,0,255,
        255,139,0,255,
        255,135,0,255,
        255,131,0,255,
        255,128,0,255,
        255,124,0,255,
        255,120,0,255,
        255,116,0,255,
        255,112,0,255,
        255,108,0,255,
        255,104,0,255,
        255,100,0,255,
        255,96,0,255,
        255,92,0,255,
        255,88,0,255,
        255,84,0,255,
        255,80,0,255,
        255,76,0,255,
        255,72,0,255,
        255,68,0,255,
        255,64,0,255,
        255,60,0,255,
        255,56,0,255,
        255,52,0,255,
        255,48,0,255,
        255,44,0,255,
        255,40,0,255,
        255,36,0,255,
        255,32,0,255,
        255,28,0,255,
        255,24,0,255,
        255,20,0,255,
        255,16,0,255,
        255,12,0,255,
        255,8,0,255,
        255,4,0,255,
        255,0,0,255,
        251,0,0,255,
        247,0,0,255,
        243,0,0,255,
        239,0,0,255,
        235,0,0,255,
        231,0,0,255,
        227,0,0,255,
        223,0,0,255,
        219,0,0,255,
        215,0,0,255,
        211,0,0,255,
        207,0,0,255,
        203,0,0,255,
        199,0,0,255,
        195,0,0,255,
        191,0,0,255,
        187,0,0,255,
        183,0,0,255,
        179,0,0,255,
        175,0,0,255,
        171,0,0,255,
        167,0,0,255,
        163,0,0,255,
        159,0,0,255,
        155,0,0,255,
        151,0,0,255,
        147,0,0,255,
        143,0,0,255,
        139,0,0,255,
        135,0,0,255,
        131,0,0,255
};

    inline void GetActualLut(const int32_t nIdDoc, BYTE LUT[256][4], const int32_t nBits) {
        uint32_t szActualLut_buffer[256];
        int nSize = sizeof(szActualLut_buffer);
        RayCi_Single_Settings_Lut_getActualLut(nIdDoc, nBits, szActualLut_buffer, &nSize);
        assert(nSize == 256);
        for (int i = 0; i < 256; ++i) {
            const uint32_t argb = szActualLut_buffer[i];
            LUT[i][0] = (argb >> 16) & 0xFF; // Red
            LUT[i][1] = (argb >> 8) & 0xFF; // Green
            LUT[i][2] = (argb >> 0) & 0xFF; // Blue
            LUT[i][3] = (argb >> 24) & 0xFF; // Alpha
        }
    }

    inline void SetNewColorTable(QImage &image) {
        QVector<QRgb> colorTable(256);
        for (int i = 0; i < 256; ++i) {
            colorTable[i] = qRgba(LUT[i][0], LUT[i][1], LUT[i][2], LUT[i][3]);
        }
        image.setColorTable(colorTable);
    }

    inline void InitImage(QImage &image, const int32_t nIdDoc, const int cx, const int cy) {
        if (image.isNull() || image.width() != cx || image.height() != cy) {
            image = QImage(cx, cy, QImage::Format_Indexed8);
            GetActualLut(nIdDoc, LUT, 8);
            SetNewColorTable(image);
        }
    }

    inline void GetBaseLut(const int32_t nIdDoc) {
        uint32_t szBaseLut_buffer[256];
        int nSize = sizeof(szBaseLut_buffer);
        RayCi_Single_Settings_Lut_getBaseLut(nIdDoc, szBaseLut_buffer, &nSize);
        assert(nSize == 256);
        for (int i = 0; i < 256; ++i) {
            const uint32_t argb = szBaseLut_buffer[i];
            LUT[i][0] = (argb >> 16) & 0xFF; // Red
            LUT[i][1] = (argb >> 8) & 0xFF; // Green
            LUT[i][2] = (argb >> 0) & 0xFF; // Blue
            LUT[i][3] = (argb >> 24) & 0xFF; // Alpha
        }
    }


}